Rails Associations - has_many => :through - but same model?

That was an interesting question I just created a working app for your use case post. Related_posts will give you all posts related from post, while post. Inverse_related_posts will give you all posts related to post Here's what my models look like: class Post "RelatedPost" has_many :related_posts, :through => :related_posts_association, :source => :related_post has_many :inverse_related_posts_association, :class_name => "RelatedPost", :foreign_key => "related_post_id" has_many :inverse_related_posts, :through => :inverse_related_posts_association, :source => :post end class RelatedPost "Post" end My schema: ActiveRecord::Schema.

Define(:version => 20110702194300) do create_table "posts", :force => true do |t| t. String "name" t. Datetime "created_at" t.

Datetime "updated_at" end create_table "related_posts", :force => true do |t| t. Integer "post_id" t. Integer "related_post_id" t.

Datetime "created_at" t. Datetime "updated_at" end end Here's a dump of a console session that demonstrating the relationship ruby-1.9.2-p180:001:0>> p = Post. Create!

Name: "5ms) INSERT INTO "posts" ("created_at", "name", "updated_at") VALUES (?,? ,? ) "created_at", Sat, 02 Jul 2011 20:03:43 UTC +00:00, "name", " "updated_at", Sat, 02 Jul 2011 20:03:43 UTC +00:00 # => # ruby-1.9.2-p180:002:0>> p2 = Post.

Create! Name: "World" SQL (1.0ms) INSERT INTO "posts" ("created_at", "name", "updated_at") VALUES (?,? ,?) "created_at", Sat, 02 Jul 2011 20:03:48 UTC +00:00, "name", "World", "updated_at", Sat, 02 Jul 2011 20:03:48 UTC +00:00 # => # ruby-1.9.2-p180:003:0>> p.

Related_posts Post Load (0.2ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts"."id" = "related_posts". "related_post_id" WHERE "related_posts".

"post_id" = 1 # => ruby-1.9.2-p180:004:0>> p2. Related_posts Post Load (0.4ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts".

"id" = "related_posts"."related_post_id" WHERE "related_posts". "post_id" = 2 # => ruby-1.9.2-p180:005:0>> p. Related_posts # ruby-1.9.2-p180:006:0>> RelatedPost.

All RelatedPost Load (0.4ms) SELECT "related_posts". * FROM "related_posts" # => # ruby-1.9.2-p180:007:0>> p2. Inverse_related_posts Post Load (0.2ms) SELECT "posts".

* FROM "posts" INNER JOIN "related_posts" ON "posts". "id" = "related_posts". "post_id" WHERE "related_posts"."related_post_id" = 2 # => # ruby-1.9.2-p180:008:0>> p = Post.

First Post Load (0.5ms) SELECT "posts". * FROM "posts" LIMIT 1 # => # ruby-1.9.2-p180:009:0>> p2. Related_posts # ruby-1.9.2-p180:010:0>> p2.

Related_posts # => # ruby-1.9.2-p180:011:0>> exit Loading development environment (Rails 3.1.0. Rc4) ruby-1.9.2-p180:001:0>> Post.first. Related_posts Post Load (0.3ms) SELECT "posts".

* FROM "posts" LIMIT 1 Post Load (0.2ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts"."id" = "related_posts". "related_post_id" WHERE "related_posts".

"post_id" = 1 # => # ruby-1.9.2-p180:002:0>> Post.last. Related_posts Post Load (0.2ms) SELECT "posts". * FROM "posts" ORDER BY "posts".

"id" DESC LIMIT 1 Post Load (0.2ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts". "id" = "related_posts".

"related_post_id" WHERE "related_posts"."post_id" = 2 # => #.

That was an interesting question. I just created a working app for your use case. Post.

Related_posts will give you all posts related from post, while post. Inverse_related_posts will give you all posts related to post. Here's what my models look like: class Post "RelatedPost" has_many :related_posts, :through => :related_posts_association, :source => :related_post has_many :inverse_related_posts_association, :class_name => "RelatedPost", :foreign_key => "related_post_id" has_many :inverse_related_posts, :through => :inverse_related_posts_association, :source => :post end class RelatedPost "Post" end My schema: ActiveRecord::Schema.

Define(:version => 20110702194300) do create_table "posts", :force => true do |t| t. String "name" t. Datetime "created_at" t.

Datetime "updated_at" end create_table "related_posts", :force => true do |t| t. Integer "post_id" t. Integer "related_post_id" t.

Datetime "created_at" t. Datetime "updated_at" end end Here's a dump of a console session that demonstrating the relationship. Ruby-1.9.2-p180:001:0>> p = Post.

Create! Name: "5ms) INSERT INTO "posts" ("created_at", "name", "updated_at") VALUES (?,? ,?) "created_at", Sat, 02 Jul 2011 20:03:43 UTC +00:00, "name", " "updated_at", Sat, 02 Jul 2011 20:03:43 UTC +00:00 # => # ruby-1.9.2-p180:002:0>> p2 = Post.

Create! Name: "World" SQL (1.0ms) INSERT INTO "posts" ("created_at", "name", "updated_at") VALUES (?,? ,?

) "created_at", Sat, 02 Jul 2011 20:03:48 UTC +00:00, "name", "World", "updated_at", Sat, 02 Jul 2011 20:03:48 UTC +00:00 # => # ruby-1.9.2-p180:003:0>> p. Related_posts Post Load (0.2ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts".

"id" = "related_posts". "related_post_id" WHERE "related_posts"."post_id" = 1 # => ruby-1.9.2-p180:004:0>> p2. Related_posts Post Load (0.4ms) SELECT "posts".

* FROM "posts" INNER JOIN "related_posts" ON "posts"."id" = "related_posts". "related_post_id" WHERE "related_posts". "post_id" = 2 # => ruby-1.9.2-p180:005:0>> p.

Related_posts # ruby-1.9.2-p180:006:0>> RelatedPost. All RelatedPost Load (0.4ms) SELECT "related_posts". * FROM "related_posts" # => # ruby-1.9.2-p180:007:0>> p2.

Inverse_related_posts Post Load (0.2ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts". "id" = "related_posts"."post_id" WHERE "related_posts".

"related_post_id" = 2 # => # ruby-1.9.2-p180:008:0>> p = Post. First Post Load (0.5ms) SELECT "posts". * FROM "posts" LIMIT 1 # => # ruby-1.9.2-p180:009:0>> p2.

Related_posts # ruby-1.9.2-p180:010:0>> p2. Related_posts # => # ruby-1.9.2-p180:011:0>> exit Loading development environment (Rails 3.1.0. Rc4) ruby-1.9.2-p180:001:0>> Post.first.

Related_posts Post Load (0.3ms) SELECT "posts". * FROM "posts" LIMIT 1 Post Load (0.2ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts".

"id" = "related_posts". "related_post_id" WHERE "related_posts"."post_id" = 1 # => # ruby-1.9.2-p180:002:0>> Post.last. Related_posts Post Load (0.2ms) SELECT "posts".

* FROM "posts" ORDER BY "posts"."id" DESC LIMIT 1 Post Load (0.2ms) SELECT "posts". * FROM "posts" INNER JOIN "related_posts" ON "posts". "id" = "related_posts"."related_post_id" WHERE "related_posts".

"post_id" = 2 # => #.

Above and beyond. Thanks. – Codeglot Jul 2 at 20:16.

You're looking for self referential association. I suggest you take inspiration here.

By default, Rails retrieves all columns. The :source option specifies the source association name for a has_many :through association. You only need to use this option if the name of the source association cannot be automatically inferred from the association name.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions